package com.baor.controller;

import com.baor.config.TokenService;
import com.baor.service.EquipmentInstructionService;
import com.baor.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;


//TODO  设备重启指令
@RestController
@RequestMapping("/instruction")
public class EquipmentInstructionController {
    @Autowired
    private EquipmentInstructionService equipmentInstructionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 获取设备指令列表
     * @return
     */
    @RequestMapping("/getList")
    public R getList(@RequestBody Map<String, Object> params){
        R r=this.equipmentInstructionService.getList(params);
        return R.ok(r);
    }
    /**
     * 添加设备指令
     * @return
     */
    @RequestMapping("/add")
    public R add(@RequestBody Map<String, Object> params, @RequestHeader(value = "token")  String token){
        if (token == null) {
            return R.er("未登录或token不存在");
        }
        Integer userId = tokenService.getUserId(token);
        if (userId == null) {
            return R.er("token无效");
        }
        params.put("operationId", userId);
        return equipmentInstructionService.add(params);
    }

    @RequestMapping("/restart/{id}")
    public R restart(@RequestHeader(value = "token")  String token,@PathVariable("id") Integer id){
        if (token == null) {
            return R.er("未登录或token不存在");
        }
        Integer userId = tokenService.getUserId(token);
        if (userId == null) {
            return R.er("token无效");
        }
        return equipmentInstructionService.restart(id,userId);
    }
}
